package 二叉树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution429 {
    public static void main(String[] args) {
//        TreeNode treeNode = new TreeNode(1);
//        TreeNode treeNode1 = new TreeNode(2);
//        TreeNode treeNode2 = new TreeNode(3);
//        treeNode.left = treeNode1;
//        treeNode.right = treeNode2;
//        System.out.println(levelOrder(treeNode));
    }

    public static List<List<Integer>> levelOrder(Node2 root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> result = new ArrayList<>();
        Queue<Node2> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while (size > 0) {
                Node2 poll = queue.poll();
                if (poll != null) {
                    list.add(poll.val);
                    if (poll.children != null) {
                        for (Node2 node2 : poll.children) {
                            queue.offer(node2);
                        }
                    }
                }
                size--;
            }
            result.add(list);
        }
        return result;
    }

}
